package com.zkyc.framework.website.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.zkyc.framework.common.Enum.LogType;
import com.zkyc.framework.common.annotation.Log;
import com.zkyc.framework.common.dto.PageDto;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.pojo.Activity;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.config.JedisPoolFactory;
import com.zkyc.framework.website.service.ActivityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.zkyc.framework.common.constant.Constant.*;

/**
 * (Activity)表控制层
 *
 * @author wj
 * @since 2022-11-09 17:10:11
 */
@RestController
@RequestMapping("activity")
public class ActivityController {
    /**
     * 服务对象
     */
    @Resource
    private ActivityService activityService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("list")
    public ResultVO queryByPage(@RequestParam("page") int page,@RequestParam("size") int size,@RequestParam("status")int  status) {
        return activityService.queryByPage(  page,size,status);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public ResultVO queryById(@RequestParam("id") Integer id) {
        Activity activity = activityService.queryById(id);
        if (activity == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        return ResultVO.data(activity);
    }

    /**
     * 新增数据
     *
     * @param activity 实体
     * @return 新增结果
     */
    @PostMapping("save")
    @Log(type = LogType.新增活动)
    public ResultVO add(@RequestBody Activity activity) {
        return activityService.insert(activity);
    }

    /**
     * 编辑数据
     *
     * @param activity 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    @Log(type = LogType.编辑活动)
    public ResultVO edit(@RequestBody Activity activity) {
        return activityService.update(activity);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("delete")
    public ResultVO deleteById(Integer id) {
        JedisPoolFactory.delete(REDIS_PREFIX + ACTIVITYCLICK + id);
        return activityService.deleteById(id);
    }


    @GetMapping("clickCount")
    public ResultVO clickCount(@RequestParam("id") Integer id) {
        JedisPoolFactory.incr(REDIS_PREFIX + ACTIVITYCLICK + id);
        return ResultVO.success("操作成功!");
    }

}

